﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.SixAcquaint.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;
using Yipusoft.Account.BLL;

public partial class BasicBase_KeyUnit_SearchKeyUnit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Where"] != null)
            {
                menu.Visible = false;
            }
            // BindDepartment("201200", "201201", "201211");

            //部门
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) == "1300")
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }
            else
            {
                DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
                DepartmentList.DataBind();
                DepartmentListBind();
            }

            BindGrid(15, 0);

        }
    }


    protected void DepartmentList_TextChanged(object sender, EventArgs e)
    {
        DepartmentListBind();
    }

    void DepartmentListBind()
    {
        if (DepartmentList.SelectedValue == "")
        {
            UnitList.Items.Clear();
            UnitList.Items.Add(new ListItem("----部门----", ""));
            UnitList.DataSource = null;
            UnitList.DataBind();
        }
        else
        {
            UnitList.Items.Clear();
            UnitList.Items.Add(new ListItem("----部门----", ""));
            string strWhere = "(departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%'  and  SUBSTRING(DepartmentID, 5, 1)<>'0') or departmentid='" + DepartmentList.SelectedValue + "'";
            UnitList.DataSource = Department.GetList(strWhere);
            UnitList.DataBind();
        }
    }

    private void BindDepartment(string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
        {
            foreach (DepartmentInfo department in Department.GetList("RIGHT(DepartmentID, 2) = '00'"))
            {
                DepartmentList.Items.Add(new ListItem(department.FullName, department.DepartmentID));
            }
        }
        else
        {
            DepartmentList.DataSource = GetDepartmentList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID, permissionLevel, permissionUnit);
            DepartmentList.DataTextField = "FullName";
            DepartmentList.DataValueField = "DepartmentID";
            DepartmentList.DataBind();

        }
    }
    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel) || Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
            departmentList = Department.GetList("DepartmentID='" + departmentID.Substring(0, 4) + "000'");
        return departmentList;
    }

    private void BindGrid(int pageSize, int pageIndex)
    {
        string where = "";
        if (Request.QueryString["Where"] != null)
        {
            where = Request.QueryString["Where"];
        }
        else
        {
            if (UnitList.SelectedValue != "")
            {
                where += where == "" ? "" : " And ";
                where += " DepartmentID='" + UnitList.SelectedValue + "' ";
            }
            else
            {
                if (DepartmentList.SelectedValue == "")
                {
                    UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
                    DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
                    if (userInfo.DepartmentID.Substring(0, 4) != "1300")
                    {
                        where += where == "" ? "" : " And ";
                        where += " DepartmentID like '" + userInfo.DepartmentID.Substring(0, 4) + "%' ";
                    }
                }
                else
                {
                    where += where == "" ? "" : " And ";
                    where += " DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ";
                }
            }
            if (txtUnitName.Text != "")
            {
                where += where == "" ? "" : " And ";
                where += "UnitName like'%" + txtUnitName.Text + "%'";
            }
        }

        if (!panPage.Visible)
            panPage.Visible = true;
        string sid = Request["sid"];

        int totalCount = KeyUnit.GetKeyUnitCountByWhere(where);
        //pageIndex = pageIndex + 1;
        ViewState["PageCount"] = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
        if ((int)ViewState["PageCount"] < pageIndex)
            pageIndex = pageIndex - 1;
        ViewState["PageIndex"] = pageIndex;

        gvKeyUnits.DataSource = KeyUnit.GetKeyUnitList(where, "DepartmentId", "Asc", pageSize, pageIndex);

        gvKeyUnits.DataBind();
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(15, int.Parse(JumpList.SelectedValue));
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindGrid(15, (int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        BindGrid(15, 0);
    }
    protected void gvKeyUnits_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblID = e.Row.FindControl("lblID") as Label;
            lblID.Text = (int.Parse(ViewState["PageIndex"].ToString()) * 15 + e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            Yipusoft.SixAcquaint.Model.KeyUnitInfo keyUnitInfo = e.Row.DataItem as Yipusoft.SixAcquaint.Model.KeyUnitInfo;
            //消火栓是否有
            HyperLink hlSystem = e.Row.FindControl("hlSystem") as HyperLink;
            if (Water.GetKeyUnitHydrantsByKeyUnitID(keyUnitInfo.KeyUnitID).Count() == 0)
            {
                hlSystem.ForeColor = System.Drawing.Color.Red;
            }

            HyperLink hlPoint = e.Row.FindControl("hlPoint") as HyperLink;
            if (KeyUnit.GetKeyPointListByKeyUnitID(keyUnitInfo.KeyUnitID).Count() == 0)
            {
                hlPoint.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
    protected void gvKeyUnits_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            string KeyUnitID = e.CommandArgument.ToString();
            if (KeyUnit.DeleteKeyUnit(KeyUnitID))
            {
                BindGrid(15, 0);
            }
        }
    }

    protected string getDepartmentName(object o)
    {
        if (o != null)
        {
            string DepartmentId = o.ToString();
            DepartmentInfo Info = Department.GetDepartment(DepartmentId);
            if (Info != null)
            {
                return Info.FullName;
            }
            else
            {
                return "";
            }
        }
        else
        {
            return "";
        }
    }
}
